package application.gui.clientWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import application.LoginAndAdministrationModule.Logging;
import application.LoginAndAdministrationModule.PersonData;
import application.LoginAndAdministrationModule.User;

public class MyDataPanel extends JPanel {

	public MyDataPanel(ClientHomePage c, PersonData p) {
		this.parent = c;
		this.user = new User(p);
		setSize(600, 480);
		setLayout(null);
		init();

	}

	private void init() {
		name = new JTextField(user.getPersonData().getName());
		name.setEditable(false);
		surname = new JTextField(user.getPersonData().getSurname());
		surname.setEditable(false);
		login = new JTextField(user.getPersonData().getLogin());
		login.setEditable(false);
		oldPass = new JPasswordField();
		newPass = new JPasswordField();
		confirmNewPass = new JPasswordField();
		email = new JTextField(user.getPersonData().geteMail());
		phone = new JTextField(user.getPersonData().getephoneNumber());
		street = new JTextField(user.getPersonData().getAddress().getStreet());
		house = new JTextField(String.valueOf(user.getPersonData().getAddress()
				.getHouseNumber()));
		flat = new JTextField(String.valueOf(user.getPersonData().getAddress()
				.getFlatNumber()));
		;
		city = new JTextField(user.getPersonData().getAddress().getCity());
		postCode = new JTextField(user.getPersonData().getAddress()
				.getPostCode());
		confirm = new JButton("Zatwierdź");
		cancel = new JButton("Anuluj");

		nameInfo = new JLabel("Imię");
		surnameInfo = new JLabel("Nazwisko");
		loginInfo = new JLabel("Login");
		oldPassInfo = new JLabel("Stare hasło");
		newPassInfo = new JLabel("Nowe hasło");
		confirmNewPassInfo = new JLabel("Powtórz nowe hasło");
		emailInfo = new JLabel("E-mail");
		phoneInfo = new JLabel("Nr telefonu");
		streetInfo = new JLabel("Ulica");
		houseInfo = new JLabel("Nr mieszkania");
		flatInfo = new JLabel("Nr bloku");
		cityInfo = new JLabel("Miasto");
		postCodeInfo = new JLabel("kod pocztowy");

		addComponent(nameInfo, 10, 20, 120, 30);
		addComponent(name, 135, 20, 100, 30);
		addComponent(surnameInfo, 250, 20, 120, 30);
		addComponent(surname, 385, 20, 100, 30);
		addComponent(oldPassInfo, 10, 55, 120, 30);
		addComponent(oldPass, 135, 55, 100, 30);
		addComponent(loginInfo, 250, 55, 120, 30);
		addComponent(login, 385, 55, 100, 30);
		addComponent(emailInfo, 250, 90, 120, 30);
		addComponent(email, 385, 90, 100, 30);
		addComponent(newPassInfo, 10, 90, 120, 30);
		addComponent(newPass, 135, 90, 100, 30);
		addComponent(phoneInfo, 250, 125, 120, 30);
		addComponent(phone, 385, 125, 100, 30);
		addComponent(confirmNewPassInfo, 10, 125, 120, 30);
		addComponent(confirmNewPass, 135, 125, 100, 30);

		addComponent(streetInfo, 10, 195, 120, 30);
		addComponent(street, 135, 195, 100, 30);
		addComponent(flatInfo, 250, 195, 120, 30);
		addComponent(flat, 385, 195, 100, 30);
		addComponent(houseInfo, 10, 230, 120, 30);
		addComponent(house, 135, 230, 100, 30);
		addComponent(cityInfo, 250, 230, 120, 30);
		addComponent(city, 385, 230, 100, 30);
		addComponent(postCodeInfo, 10, 265, 120, 30);
		addComponent(postCode, 135, 265, 100, 30);

		addComponent(cancel, 300, 330, 100, 40);
		addComponent(confirm, 120, 330, 100, 40);

		setListeners();
	}

	private void addComponent(JComponent comp, int x, int y, int width,
			int height) {
		add(comp);
		comp.setBounds(x, y, width, height);
	}

	private void setListeners() {
		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				parent.registerMsg(3);
			}
		});

		confirm.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (String.valueOf(newPass.getPassword()).equals(
						String.valueOf(confirmNewPass.getPassword()))) {
					try {
						if (user.change_password(new Logging(),
								String.valueOf(oldPass.getPassword()),
								String.valueOf(newPass.getPassword()))){
							JOptionPane.showMessageDialog(parent, "Hasło zostało zmienione");
						}
						else{
							JOptionPane.showMessageDialog(parent, "Stare hasło jest nieprawidłowe","Error",JOptionPane.ERROR_MESSAGE);
						}
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else {
					JOptionPane pane = new JOptionPane();
					pane.showMessageDialog(parent,
							"Nieprawidłowe potwierdzenie hasła","Error",JOptionPane.ERROR_MESSAGE);
				}
				
				try {
					user.edit_user_data("eMail", email.getText());
					user.edit_user_data("phoneNumber",phone.getText());
					user.edit_user_address("houseNumber", house.getText());
					user.edit_user_address("flatNumber", flat.getText());
					user.edit_user_address("street", street.getText());
					user.edit_user_address("city", city.getText());
					user.edit_user_address("postCode", postCode.getText());
					JOptionPane.showMessageDialog(parent, "Dane osobowe zostały zmienione", "OK", JOptionPane.INFORMATION_MESSAGE);
					oldPass.setText("");
					newPass.setText("");
					confirmNewPass.setText("");
					setVisible(false);
					parent.getMainPanel().setVisible(true);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
	}

	private ClientHomePage parent;
	private User user;

	private JTextField name;
	private JTextField surname;
	private JTextField login;
	private JPasswordField oldPass;
	private JPasswordField newPass;
	private JPasswordField confirmNewPass;
	private JTextField email;
	private JTextField phone;
	private JTextField street;
	private JTextField house;
	private JTextField flat;
	private JTextField city;
	private JTextField postCode;

	private JButton confirm;
	private JButton cancel;

	private JLabel nameInfo;
	private JLabel surnameInfo;
	private JLabel loginInfo;
	private JLabel oldPassInfo;
	private JLabel newPassInfo;
	private JLabel confirmNewPassInfo;
	private JLabel emailInfo;
	private JLabel phoneInfo;
	private JLabel streetInfo;
	private JLabel houseInfo;
	private JLabel flatInfo;
	private JLabel cityInfo;
	private JLabel postCodeInfo;

}
